<?php
/**
 * 二期上传图片接口
 */
namespace app\api\controller;
use think\Controller;
use think\Request;
use think\Db;
class Upload extends Controller{
	/**
	 * 上传普通图片接口
	 */
	public function upload(){
		header("Access-Control-Allow-Origin: *");
    	header("Access-Control-Allow-Methods: OPTIONS,HEAD,GET,POST,PUT,PATCH,DELETE");
   		header("Access-Control-Allow-Headers:Content-Type,Content-Range,Content-Disposition");
		$oldpath = !empty(request()->param("path")) ? request()->param("path") : 'images';
		if(isset($_REQUEST['phone'])){
			$phone = $_REQUEST['phone'];
		}else{
			$phone = "";
		}
		if(isset($_REQUEST['system'])){
			$system = $_REQUEST['system'];
		}else{
			$system = "";
		}
		$path = "../public/uploads/".$oldpath;
		$fileArr='';										//返回的图片保存路径序列化字符串
		// 获取表单上传文件
		//$files = request()->file('file');
		if($system=="android"){
			$files = request()->file();
		}else{
			$files = request()->file('file');
		}
		if(!$files){
			return json(['code' => 500, 'msg' => '图片上传失败', 'data' =>$fileArr]);
		}
		try{
			foreach($files as $file){
				$info = $file->move($path);
				if($info){
					$fileArr.=$oldpath.'/'.$info->getSaveName().',';
				}
			}
		} catch (Exception $e) {
			return json(['code' => 501, 'msg' => '图片上传失败', 'data' =>$fileArr]);
		}
		$fileArr=rtrim($fileArr,',');
		return json(['code' => 200, 'msg' => '图片上传成功', 'data' =>$fileArr]);
	}
}